package com.yulongtian.weekCompetition.month202303.week01;

import java.util.*;

/**
 * 可
 *
 * @author yulongTian
 * @create 2023-03-05 10:38
 */
public class Test12 {
    public static void main(String[] args) {
        //2147483647
        //4012835991
        System.out.println(Integer.MAX_VALUE);
    }

    //4012835991
    public long kthLargestLevelSum(TreeNode root, int k) {
        //层序遍历
        Queue<TreeNode> queue = new ArrayDeque<>();
        queue.add(root);
        List<Long> per_level_sum = new ArrayList<>();
        while (!queue.isEmpty()) {
            int len = queue.size();
            long temp_sum = 0L;
            for (int i = 0; i < len; i++) {
                TreeNode poll = queue.poll();
                temp_sum += poll.val;
                if (poll.left != null) {
                    queue.add(poll.left);
                }
                if (poll.right != null) {
                    queue.add(poll.right);
                }
            }
            per_level_sum.add(temp_sum);
        }
        per_level_sum.sort(new Comparator<Long>() {
            @Override
            public int compare(Long o1, Long o2) {
                if (o2 > o1) {
                    return 1;
                } else if (o2.equals(o1)) {
                    return 0;
                } else {
                    return -1;
                }
            }
        });
        if (per_level_sum.size() < k) {
            return -1;
        } else {
            return per_level_sum.get(k - 1);
        }

    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

}
